/**
 * JS实用工具 
 */

Ext.apply(hjp.utils, {
    lowercase : function(str) {
        if (Ext.isEmpty(str)) {
            return str;
        }
        str = str.toString();
        return str.toLowerCase();
    },
    lowercaseFirstChar : function(str) {
        if (Ext.isEmpty(str)) {
            return str;
        }
        str = str.toString();
        return str.charAt(0).toLowerCase() + str.slice(1);
    },
    capitalize : function(str) {
        if (Ext.isEmpty(str)) {
            return str;
        }
        str = str.toString();
        return str.charAt(0).toUpperCase() + str.slice(1);
    },
    returnEmptyStr : function() {
        return '';
    },
    convert : {
        stringContextToBool : function(str) {
            return (str.toLowerCase() === 'true');
        }
    },
    // deep copy of an object. All references independent from object passed
    // in.
    cloneObj : function(o) {
        if (typeof(o) != 'object' || o === null) {
            return o;
        }

        var newObj = {};

        for (var i in o) {
            newObj[i] = hjp.utils.cloneObj(o[i]);
        }

        return newObj;
    },

    // (Array : arr, string : child, [string seperator])
    // array to join, name of element of contained object, seperator
    // (defaults to ", ")
    joinArrayObject : function(arr, child, seperator) {
        var sOut = '';
        var sep = (seperator) ? seperator : ', ';

        for (var i = 0; i < arr.length; i++) {
            if ((arr[i])[child]) {
                sOut += (arr[i])[child] + sep;
            }
        }

        return sOut.substring(0, sOut.length - sep.length);
    },
    getLastTime : function(v) {
        if (v.length < 18)
            return "";
        var d = Date.parseDate(v, 'Y-m-d H:i:s');
        var m = d.getElapsed();
        var minutes = Math.floor(m / 1000 / 60);
        var seconds = Math.floor(m / 1000) % 60;
        var hours = Math.floor(m / 1000 / 60 / 60);
        var days = Math.floor(m / 1000 / 60 / 60 / 24);
        var time = "";
        if (days > 30)
            time = v.substring(0, 10);
        else if (days > 0)
            time = days + '天前';
        else if (hours > 0)
            time = hours + '小时前';
        else if (minutes > 0)
            time = minutes + '分钟前';
        else
            time = seconds + '秒前';
        return time;
    }

});
// EOP

/**
 * 检测并提示大写状态 
 * Thanks: http://17thdegree.com/wp/capslock.html
 */
hjp.utils.CapsLock = function() {
    return {
        init : function() {
            var id = Ext.id();
            this.alertBox = Ext.DomHelper.append(document.body, {
                tag : 'div',
                style : 'width:10em',
                children : [{
                    tag : 'div',
                    style : 'text-align: center; background-color: black;color: white;',
                    html : '现在是字母大写状态',
                    id : id
                }]
            }, true);
            Ext.fly(id).boxWrap();
            this.alertBox.hide();

            var pwds = Ext.query("INPUT[type='password']");
            for (var i = 0;i < pwds.length; i++) {
                Ext.get(pwds[i].id)
                        .on(
                                'keypress',
                                this.keypress.createDelegate(this, pwds[i],
                                        true), this);
            }
        },
        keypress : function(e, el) {
            var charCode = e.getCharCode();
            if ((e.shiftKey && charCode >= 97 && charCode <= 122)
                    || (!e.shiftKey && charCode >= 65 && charCode <= 90)) {
                this.showWarning(el);
            } else {
                this.hideWarning();
            }
        },
        showWarning : function(el) {
            var x = Ext.fly(el).getX();
            var width = Ext.fly(el).getWidth();
            var y = Ext.fly(el).getY();

            this.alertBox.setXY([x + width + 100, y]);
            this.alertBox.show();
        },
        hideWarning : function() {
            this.alertBox.hide();
        }
    }
    
}()
//EOP

/**
 * 客户端持久存储
 */
hjp.storeMode = 1 // 1-cookies

if (hjp.storeMode == 1) {
    Ext.apply(hjp, {
        // save data in store
        save : function(name, value) {
            hjp.setCookie(name, value);
        },
        find : function(name) {
            return hjp.getCookie(name);
        },
        remove : function(name) {
            hjp.delCookie(name);
        },
        clearRememberUserInfo : function() {
            hjp.remove("login_remember");
            hjp.remove("login_username");
            hjp.remove("login_password");
        },
        setCookie : function(name, value) {
            var Days = 365;
            var exp = new Date();
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
            document.cookie = name + "=" + escape(value) + ";expires="
                    + exp.toGMTString();
        },
        getCookie : function(name) {
            var arr = document.cookie.match(new RegExp("(^| )" + name
                    + "=([^;]*)(;|$)"));
            if (arr != null) {
                return unescape(arr[2]);                
            }
            return null;
        },
        delCookie : function(name) {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval = hjp.getCookie(name);
            if (cval != null) {
                document.cookie = name + "=" + cval + ";expires="
                        + exp.toGMTString();                
            }
        }
        
    })
}